package per.chy.stard.controller;


import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import per.chy.stard.entity.Page;
import per.chy.stard.entity.Standard;
import per.chy.stard.service.StandService;


@Controller
public class StandController {
	
	@Autowired
	private StandService stds;
	
	@RequestMapping("/std.html")
	public ModelAndView showBookInfo(String likeName,Integer pageNo,Integer pageSize){
		if(pageNo==null||"".equals(pageNo)||pageNo==0){
			pageNo=1;
		}
		if(pageSize==null||"".equals(pageSize)||pageSize==0){
			pageSize=3;
		}
		
		ModelAndView mv = new ModelAndView();
		//设置跳转页面_配置的为逻辑视图名，通过视图解析器转换成真实视图路径
		
		//分页参数
		Integer totalCount = stds.getTotalCountLikeName(likeName);
		Integer pageCount = (totalCount+pageSize-1)/pageSize;
		Page page = new Page();
	  	System.out.println("std4==================>"+page.getPageCount());
	  	if(pageNo<1){
	  		pageNo=1;
	  	}
	  	if(pageNo>pageCount){
	  		pageNo=pageCount;
	  	}
	  	List<Standard> std = stds.findByPageLikeName(likeName,(pageNo-1)*pageSize, pageSize);
	  	page.setPageCount(pageCount);
	  	page.setTotalCount(totalCount);
	  	page.setPageNo(pageNo);
	  	page.setPageSize(pageSize);
	  	
	  //获取列表数据
  		
		mv.setViewName("standInfo");
		mv.addObject("std",std);
		mv.addObject("page", page);
		mv.addObject("likeName", likeName);
		return mv;
	}
	
	@RequestMapping("/add.html")
	public ModelAndView addStd(){
		ModelAndView mv = new ModelAndView();
		//设置跳转页面_配置的为逻辑视图名，通过视图解析器转换成真实视图路径
		mv.setViewName("add1");
		return mv; 
	}
	
	@RequestMapping("/add2.html")
	public ModelAndView addStdInfo(String stdNum,String zhname,String version,String keys,Date implDate,Date releaseDate,String packagePath){
		ModelAndView mv = new ModelAndView();
		Standard std = new Standard();
		std.setStdNum(stdNum);
		std.setZhname(zhname);
		std.setVersion(version);
		std.setKeys(keys);
		std.setImplDate(implDate);
		std.setReleaseDate(releaseDate);
		std.setPackagePath(packagePath);
		int num = stds.added(std);
		System.out.println("============>"+num);
		if(num>0){
			mv.setViewName("redirect:/std.html");
		}else{
			mv.setViewName("redirect:/add.html");
		}
		return mv; 
	}
	
	//删除数据
	@RequestMapping("/deleteInfo.html")
	public ModelAndView delStdInfo(Integer[] id,String likeName,Integer pageNo,Integer pageSize){
		ModelAndView mv = new ModelAndView();
		System.out.println("----------->"+id.length);
		int num=0;
		if(id!=null&&!"".equals(id)){
			for(int i:id){
				System.out.println("----------->"+i);
			}
			for(int i:id){
				num = stds.delete(i);
			}
		}
		if(num>0){
			mv.setViewName("redirect:/std.html");
			mv.addObject("pageNo", pageNo);
			mv.addObject("pageSize", pageSize);
			mv.addObject("likeName", likeName);
		}
		return mv; 
	}
	
	//修改数据
	@RequestMapping("/update1.html")
	public ModelAndView updateStd(Integer id,String likeName,Integer pageNo,Integer pageSize){
		System.out.println("updateStd===============>"+id);
		ModelAndView mv = new ModelAndView();
		mv.setViewName("updateInfo");
		Standard std = stds.showStandById(id);
		mv.addObject("std",std);
		mv.addObject("pageNo", pageNo);
		mv.addObject("pageSize", pageSize);
		mv.addObject("likeName", likeName);
		return mv; 
	}
	
	@RequestMapping("/update2.html")
	public ModelAndView updateStdInfo(Integer id,String stdNum,String zhname,String version,String keys,Date implDate,Date releaseDate,String packagePath,String likeName,Integer pageNo,Integer pageSize){
		ModelAndView mv = new ModelAndView();
		Standard std = new Standard();
		std.setId(id);
		std.setStdNum(stdNum);
		std.setZhname(zhname);
		std.setVersion(version);
		std.setKeys(keys);
		std.setImplDate(implDate);
		std.setReleaseDate(releaseDate);
		std.setPackagePath(packagePath);
		int num = stds.update(std);
		System.out.println("============>"+num);
		
		if(num>0){
			mv.setViewName("redirect:/std.html");
		}else{
			mv.setViewName("redirect:/update1.html");
		}
		return mv;
	}

}
